#ifndef _BT_NET_LAYER_
#define _BT_NET_LAYER_

#include "cocos2d.h"
USING_NS_CC;
#include "btSocket.h"
#include "btNetStruct.h"
#include "../pthreadcc/pthreadcc.h"
#include "BattleInfoMgr.h"
class CNetHandler : public cocos2d::CCNode
{
public:
	enum{
		//RETRY_CONNECT_TOTAL_TIME_BEFORE_HINT = 100,//弹出错误对话框之前
		RETRY_CONNECT_TOTAL_TIME_BEFORE_HINT = 1,//弹出错误对话框之前[2013/08/08 ZZB]
	};
	enum{
		STEP_INIT,   //初始
		STEP_CONNECT,//连接
		STEP_MAIN,//数据收阶段
		STEP_NR
	};
	static CNetHandler* sharedNetHandler();
	virtual bool init();
	virtual void update(float dt);
	
	void Quit();
	/*void setHelloworld(HelloWorld*pHelloScene){
     m_pHelloworld = pHelloScene;
     }*/
	//创建线程
	int CreateConnectThread();
	void reconnet();
	void OnConnectFinished(int ret,int ecode);
	bool isConnected(){return m_bIsConnected;}
	int  getConnectErrorTime(){return m_connectErrorTime;}
	int CreateRecvThread();
	void onNetError(int len,int errorcode);
	//ÔÝÊ±²»Òª
	//int CreateSendThread();
	//socket 操作
	CSocket* GetSocket(){return m_pSocket;}
	void ReleaseSocket();
	void resetSocket();
	//解析
	void parseNetRecvData(const char* pBuffer,int buflen,int ecode);
	_netEventStructBase* parseOneRecvCommand(std::string& commandstr);
	void addEvent(_netEventStructBase*pEvent);
	//发送数据
	bool SendEventCreateUser(const std::string& m_gameCenterId,const std::string& m_userName);
	bool SendEventLogIn(int m_userID);
	bool SendEventLogOut(/*int m_userID*/);
	bool SendEventGetPlayerMapInfo(int m_userID,int nFirstLoad = 0);
	//bool SendEventSavePlayerMapInfo(int m_userID);// todo... [2013/3/15 14:59 butcher]
    
    //设置灵石的消息(包括用户ID灵石数量，更具灵石数量判断是查询还是增加删除)[2013/06/05 ZZB]
    bool SendEventChangeLingshiCount(int m_userID,int LingshiNr);
	//COC wwj
	bool SendEventSavePlayerMapInfo(int m_userID,const std::string& mapData);// todo... [2013/3/15 14:59 butcher]
	//创建用户名
    bool SendEventCreateUserName(int iUserID, const std::string& strUserName);
	//
	bool SendEventShowFriendsList(int m_userID);
	bool SendEventQueryUsers(const std::string& m_userName);
	bool SendEventAddFriend(int m_userID,int m_friendID);
	bool SendEventAcceptAddFriend(int m_userID,int m_friendID);
	bool SendEventQueryOfflineAddFriendMsg(int m_userID);
	bool SendEventDeleteFriend(int m_userID,int m_friendID);
	bool SendEventSendFriendMsg(int m_userID,int m_friendID,const std::string& msg);
	//bool SendEventVisit(int m_visitUserID);
	//
	bool SendEventQueryTeam(void);
	bool SendEventAddTeam(int m_userID,int m_teamID,int targetid,int nMaxLevelHeroType);
	bool SendEventCreateTeam(int m_userID,int m_teamGoalID,int nMaxLevelHeroType);
	bool SendEventLeaveTeam(int m_userID,int m_teamID);
	bool SendEventDeleteUser(int m_playerID,int teamid);
	bool SendEventSendTeamMsg(int m_userID,int m_teamID,const std::string& msg);
	bool SendEventInviteToTeam(int m_teamID,int m_userID,int m_invitedID,const std::string& username);
	bool SendEventDisMissTeam(int m_teamID,int userID);
	bool SendEventAcceptEnterTeam(int m_teamID,int userID,int nMaxLevelHeroType);
	bool SendEventRefuseInviteToTeam(int m_teamID,int userID);
	bool SendEventQueryMyTeam(int userID);
	bool SendEventShowOnlinePlayers(int userID);
	bool SendEventShowOnlineFriends(int userID);
	bool SendEventShowOnlineClanPlayers(int userID);
    
	//
	bool SendEventQueryUnions(const std::string& m_unionName);
	bool SendEventApplyIntoUnion(int m_unionID,int m_userID);
	bool SendEventCreateUnion(
                              int m_userID,
                              const std::string& m_unionName,
                              int icon,
                              const std::string& m_unionDesc,
                              int m_acceptMemberType,
                              int m_acceptJoinMinScore
                              );
	bool SendEventShowUnion(int m_unionID);
	bool SendEventSendUnionMsg(int m_unionID,int m_userID,const std::string& msg);
	bool SendEventDismissUnion(int m_userID);
	bool SendEventQueryTopUnions(void);
	bool SendEventLeaveUnion(int m_userID);
	//note:这里的时间是告诉服务器,这条消息之后的100条发送过来
	//现在默认为0,让服务器发送最新的,不要设置时间了
	bool SendEventQueryUnionMsg(int m_userID,int m_time = 0);
	bool SendEventDeleteUnionMember(int leaderID,int m_userID);
	bool SendEventBroadUnionMsg(int m_userID,int msg_type);
	bool SendEventModifyUnionMemberRole(const _oneUnionMemberModifyInf& inf);	
	bool SendEventBatchModifyUnionMembers(int modifyuserid,const std::vector<_oneUnionMemberModifyInf>& infvec);
	//common building
	bool SendEventCorAttack(int teamid, int selectMapID);
	bool SendEventCorBuild(int teamid);
	bool SendEventStartBuilding(int userid,int teamid,const std::string& mapData);
	bool SendEventGetTeamMapInfo(int userid,int teamid);
	//building
	bool SendEventBuildingBuild(int userid,int buildingtype,int usertilenr,int tilex,int tiley);
	bool SendEventBuildingBuildFinished(int userid,int tileid,int buildingtype);
	bool SendEventBuildingMoveEnd(int userid,int tileid,int buildingtype,int usertilenr,int tilex,int tiley,int ftilex,int ftiley);
	bool SendEventBuildingUpgrade(int userid,int tileid,int buildingtype,int nowlevel);
	bool SendEventBuildingUpgradeFinished(int userid,int tileid,int buildingtype,int nowlevel);
	bool SendEventBuildingDecBlood(int userid,int tileid,int buildingtype,float nowblood);
	//magic
	bool SendEventMagicStart(int userid,CCPoint& pos,int tilex,int tiley);
	//soldier
	bool SendEventSoldierMovetoAttack(int userid,const std::vector<_oneSoldierAttackMoveInf>& infvec);
	bool SendEventSoldierDead(int userid,int soldierID);
	//  [2013/6/28 12:29 butcher]
	bool SendEventKeepAlivePing();
	bool SendEventSaveMapAndBroadcast(int userid,const std::string& mapData);
    bool SendEventHeroSkill(int userid, int heroid, int skillid, const CCPoint& pt, int spec = 0);
	//COCNET wwj
	bool SendEventBattleSearchPlayer(int userid, int userlevel, int userscore);
    bool SendEventSearchOnePlayer(int useid);
    bool SendEventGetLatestReplays(int userid);
	bool SendEventGetReplays(int userid, int attackid, int battleid);
	bool SendEventAddBattleInfo(int defendid, int attackid, BattleInfo &info, const std::string& battleinfo);
	bool SendEventRevenge(int attackid, int defendid);
    bool SendEventgetUserRanks(int userid);
    bool SendEventGetSelectHeroTimes(int userid);
    bool SendEventUpdateSelectHeroTimes(int userid, int times);
    bool SendEventSaveSelectHeroTiming(int userid, int hunLastTimes, std::string& hunPreTiming, int thouLastTimes, std::string& thouPreTiming);
    bool SendEventGrabResources(int id, std::vector<int>& idVec, int* headArr);
	//启动游戏的时候获取服务器捐兵消息
    bool SendEventGetDonatorDonationMessage(int userID);
	////function: donate army
	//add by jason [2013/05/02]
	//add by jason [2013/4/26] 发送派兵请求事件
	bool SendEventApplyPutSoldiersInfo();
	//add by jason [2013/4/27] 发送派兵事件
	bool SendEventSendSoldiersInfo(int userIdRequest, int msgId, std::string armysNum, int armysHouse, std::string armysType, std::string armysDegree);
    bool SendEventReconnectServer(int m_userID);//[2013/07/31]
    int m_nKeepAlivePingSendCount;
    
    void setUpdateEventQueueStop(bool bIfUpdateEventQueue){m_bIfUpdateEventQueue = bIfUpdateEventQueue;}
    bool ifUpdateEventQueue(){return m_bIfUpdateEventQueue;}
    //hsx 2013.10.21
    bool SendSysTime();
private:
	CNetHandler();
	virtual ~CNetHandler();
	void updateEventQueue(float dt);
	void LockMutex() { m_mutex.Lock();}
	void UnLockMutex() { m_mutex.Unlock(); }
private:
	int m_step;
	CSocket* m_pSocket;
	std::list<_netEventStructBase*> m_netEventQueue;
	MutualExclusion m_mutex;
	//HelloWorld* m_pHelloworld;
	bool m_bIsConnected;
	int m_connectErrorTime;
    bool m_bIfUpdateEventQueue;
    std::string m_sSysTime;//hsx 增加系统时间 2013.10.21
public:
    void setSysTime(std::string sTime){m_sSysTime = sTime;}
    std::string getSysTime(){return m_sSysTime;}
};

#endif